package com.example.home.logistics.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class AddComplaintActivity extends AppCompatActivity {
    private ImageView back;
    private TextView jsonData;
    private TextView infoNo;
    private EditText description;
    private Button btn_send;
    private EditText inputField;
    private String companyId;
    private String infoNo1;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_complaint);

        Intent intent = getIntent();
        companyId = intent.getStringExtra("companyId");
        infoNo1 = intent.getStringExtra("infoNo");
        name = intent.getStringExtra("name");
        initView();
        sendComplaint();
    }

    private void sendComplaint() {
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strDescription = description.getText().toString();
                String questionType = inputField.getText().toString();
                JSONObject jsonObject = new JSONObject();
                if (!TextUtils.isEmpty(strDescription)){
                    try {
                        jsonObject.put("companyId",companyId);
                        jsonObject.put("infoNo",infoNo1);
                        jsonObject.put("questionType",questionType);
                        jsonObject.put("description",strDescription);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                OKHttpTool.doNetRequest("prod-api/api/logistics-inquiry/logistics_complaint",
                        OKHttpTool.POST,
                        GeneralClass.class,
                        new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                GeneralClass generalClass = (GeneralClass) obj;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (generalClass.getCode() == 200){
                                            Toast.makeText(AddComplaintActivity.this,"提交成功", Toast.LENGTH_SHORT).show();
                                            AddComplaintActivity.this.finish();
                                        }else if (generalClass.getCode() == 500){
                                            Toast.makeText(AddComplaintActivity.this,"提交失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFailure(String msg) {

                            }
                        },jsonObject.toString());
            }
        });
    }


    private void initView() {

        back = findViewById(R.id.back);
        jsonData = findViewById(R.id.jsonData);
        infoNo = findViewById(R.id.infoNo);
        description = findViewById(R.id.description);
        inputField = findViewById(R.id.inputField);
        btn_send = findViewById(R.id.btn_send);

        jsonData.setText(name);
        infoNo.setText(infoNo1);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddComplaintActivity.this.finish();
            }
        });
    }
}